package com.alibaba.citrus.ots.forecast.functions.forecastorder.factory;

import com.alibaba.citrus.ots.common.util.SpringContextUtil;
import com.alibaba.citrus.ots.forecast.functions.forecastorder.entity.ForecastOrderLineEntity;
import com.alibaba.citrus.ots.forecast.repository.ForecastOrderLineQueryRepository;
import com.alibaba.citrus.ots.forecast.repository.ForecastOrderLineWriteRepository;
import com.alibaba.cz.base.tool.Nullable;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.fastjson.JSONObject;
import com.epoch.app.otsforecastsales.dto.request.BatchAnalysisOrdersRequest;
import com.epoch.app.otsforecastsales.model.enums.ForecastDealStatusSEO;
import com.epoch.app.otsforecastsales.sdo.ForecastOrderLineSDO;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author yuanDeng
 * @Date 2021/9/15 21:41
 **/
public class ForecastOrderLineFactory {
    private static ForecastOrderLineQueryRepository forecastOrderLineQueryRepository;
    private static ForecastOrderLineWriteRepository forecastOrderLineWriteRepository;

    static {
        forecastOrderLineQueryRepository = SpringContextUtil.getBean(ForecastOrderLineQueryRepository.class);
        forecastOrderLineWriteRepository = SpringContextUtil.getBean(ForecastOrderLineWriteRepository.class);
    }

    /**
     * 从库中加载请求的子单信息，并判断子单是否正在分析中，如果子单在分析中，则返回null
     *
     * @param batchAnalysisOrdersRequest
     * @return 如果子单状态已经是分析中，则返回null
     */
    public static List<ForecastOrderLineEntity> loadNotAnalyzingOrderLineListByIds(
            BatchAnalysisOrdersRequest batchAnalysisOrdersRequest) {
        PageResult<List<ForecastOrderLineSDO>> pageResult = forecastOrderLineQueryRepository.batchLoadList(batchAnalysisOrdersRequest.getIds());
        List<ForecastOrderLineSDO> forecastOrderLineList = pageResult.getContent();
        if (Objects.isNull(forecastOrderLineList)) {
            return null;
        }
        List<ForecastOrderLineSDO> forecastOrderLineSDOList = new ArrayList<>();
        try {
            for (ForecastOrderLineSDO forecastOrderLineSDO : forecastOrderLineList) {
                //20220314，防止分析中的子单再次进行分析
                if (forecastOrderLineSDO.getDealStatus().equals(ForecastDealStatusSEO.ANALYSIS_ING.getStatus())) {
                    return null;
                }
                //修改状态
                ForecastOrderLineSDO forecastOrderLine = JSONObject.parseObject(JSONObject.toJSONString(forecastOrderLineSDO), ForecastOrderLineSDO.class);
                forecastOrderLine.setDealStatus(ForecastDealStatusSEO.ANALYSIS_ING.getStatus());
                forecastOrderLine.setGmtModified(new Date());
                forecastOrderLine.setModifierId(batchAnalysisOrdersRequest.getModifierId());
                forecastOrderLine.setModifierName(batchAnalysisOrdersRequest.getModifierName());
                forecastOrderLineSDOList.add(forecastOrderLine);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Nullable.stream(forecastOrderLineSDOList).map(ForecastOrderLineEntity::of).collect(Collectors.toList());
    }

    /**
     * 根据子单id加载子单信息
     *
     * @param batchAnalysisOrdersRequest
     * @return
     */
    public static List<ForecastOrderLineEntity> loadOrderLinesByIds(
            BatchAnalysisOrdersRequest batchAnalysisOrdersRequest) {
        PageResult<List<ForecastOrderLineSDO>> pageResult = forecastOrderLineQueryRepository.batchLoadList(batchAnalysisOrdersRequest.getIds());
        List<ForecastOrderLineSDO> forecastOrderLineList = pageResult.getContent();
        if (Objects.isNull(forecastOrderLineList)) {
            return null;
        }

        return Nullable.stream(forecastOrderLineList).map(ForecastOrderLineEntity::of).collect(Collectors.toList());
    }
    /*
    public static List<ForecastOrderLineEntity> loadForecastOrdersByIdList( List<String> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return null;
        }
        PageResult<List<ForecastOrderLineSDO>> pageResult = forecastOrderLineQueryRepository.batchLoadList(idList);
        List<ForecastOrderLineSDO> content = pageResult.getContent();
        if (Objects.isNull(content)){
            return null;
        }
        List<ForecastOrderLineSDO> forecastOrderLineSDOList = new ArrayList<>();
        try {
            for (ForecastOrderLineSDO forecastOrderLineSDO : content) {
                //修改状态
                ForecastOrderLineSDO forecastOrderLine = JSONObject.parseObject(JSONObject.toJSONString(forecastOrderLineSDO), ForecastOrderLineSDO.class);
                forecastOrderLine.setDealStatus(ForecastDealStatusSEO.ANALYSIS_ING.getStatus());
                forecastOrderLine.setGmtModified(new Date());
                forecastOrderLine.setModifierId(batchAnalysisOrdersRequest.getModifierId());
                forecastOrderLine.setModifierName(batchAnalysisOrdersRequest.getModifierName());
                forecastOrderLineSDOList.add(forecastOrderLine);
            }
        } catch (Exception e){
            e.printStackTrace();
        }

        return Nullable.stream(forecastOrderLineSDOList).map(ForecastOrderLineEntity::of).collect(Collectors.toList());
    }

    public static List<ForecastOrderLineEntity> updateReplyQuantity(BatchAnalysisOrdersRequest batchAnalysisOrdersRequest) {
        PageResult<List<ForecastOrderLineSDO>> pageResult = forecastOrderLineQueryRepository.batchLoadList(batchAnalysisOrdersRequest.getIds());
        List<ForecastOrderLineSDO> content = pageResult.getContent();
        for (Object obj : content) {
            //订单进行分析分析后赋值，报保存
            ForecastOrderLineSDO forecastOrderLine =JSONObject.parseObject(JSONObject.toJSONString(obj),ForecastOrderLineSDO.class);
//            String id = forecastOrderLine.getId();
//            String[] split = forecastOrderLine.getId().split(".");
//            forecastOrderLine.setId(forecastOrderLine.getId().split(".")[0]);
            forecastOrderLine.setPredictReplyAmount(99);
            forecastOrderLine.setRefuseReason("感觉不好");
            forecastOrderLine.setStatus(2);//2回复完成
            boolean b1 = forecastOrderLineWriteRepository.updateReplyQuantity(forecastOrderLine);
        }

        //List<ForecastOrderLineEntity> collect = Nullable.stream(pageResult.getContent()).map(ForecastOrderLineEntity::of).collect(Collectors.toList());
        return null;
    }


    public static List<ForecastOrderLineEntity> loadDealStatusList(List<Integer> dealStatus) {
        PageResult<List<ForecastOrderLineSDO>> pageResult = forecastOrderLineQueryRepository.queryListByDealStatus(dealStatus);
        List<ForecastOrderLineSDO> content = pageResult.getContent();
        List<ForecastOrderLineSDO> forecastOrderLineSDOList = new ArrayList<>();
        for (int i = 0; i < content.size(); i++) {
            //修改状态
            ForecastOrderLineSDO forecastOrderLine = JSONObject.parseObject(JSONObject.toJSONString(content.get(i)), ForecastOrderLineSDO.class);
            forecastOrderLine.setStatus(ForecastDealStatusSEO.WAITING_DEAL.getStatus());
            forecastOrderLineSDOList.add(forecastOrderLine);
        }
        List<ForecastOrderLineEntity> collect = Nullable.stream(forecastOrderLineSDOList).map(ForecastOrderLineEntity::of).collect(Collectors.toList());
        return collect;
    }
     */
}
